package kr.hkit.powerjava.coffee;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import kr.hkit.powerjava.coffee.common.ConnectionFactory;
import javax.swing.JLabel;
import java.awt.Font;

public class Prn1 extends JFrame {

	private JPanel contentPane;
	private JScrollPane scrollPane;
	private JTable table;
	private JLabel title;

	public Prn1(Connection connection, ResultSet rs) throws SQLException {
		initialize();
		tableCellAlginAndWidth();
		prn(connection, rs);
	}

	private void tableCellAlginAndWidth() {
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		DefaultTableCellRenderer dtcrLeft = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.LEFT);
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);

		TableColumnModel cModel = table.getColumnModel();

		int columnCount = cModel.getColumnCount();// 컬럼의 개수

		cModel.getColumn(0).setPreferredWidth(100);
		cModel.getColumn(0).setCellRenderer(dtcrCenter);

		cModel.getColumn(1).setPreferredWidth(180);
		cModel.getColumn(1).setCellRenderer(dtcrCenter);

		cModel.getColumn(2).setPreferredWidth(250);
		cModel.getColumn(2).setCellRenderer(dtcrLeft);

		cModel.getColumn(3).setPreferredWidth(180);
		cModel.getColumn(3).setCellRenderer(dtcrRight);

		cModel.getColumn(4).setPreferredWidth(180);
		cModel.getColumn(4).setCellRenderer(dtcrRight);

		cModel.getColumn(5).setPreferredWidth(250);
		cModel.getColumn(5).setCellRenderer(dtcrRight);

		cModel.getColumn(6).setPreferredWidth(250);
		cModel.getColumn(6).setCellRenderer(dtcrRight);

		cModel.getColumn(7).setPreferredWidth(250);
		cModel.getColumn(7).setCellRenderer(dtcrRight);

		cModel.getColumn(8).setPreferredWidth(180);
		cModel.getColumn(8).setCellRenderer(dtcrRight);

		cModel.getColumn(9).setPreferredWidth(250);
		cModel.getColumn(9).setCellRenderer(dtcrRight);

		cModel.getColumn(4).setPreferredWidth(200);
		cModel.getColumn(4).setCellRenderer(dtcrRight);
	}

	private void prn(Connection connection, ResultSet rs) throws SQLException {
		DefaultTableModel model = (DefaultTableModel) table.getModel();

		int count = 0;
		int temp = 0;	//순위 아직 안함!
		Object[] newRow = null;
		while (rs.next()) {
			count++;
			newRow = new Object[] { count, rs.getString(2), rs.getString(1),
					rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6),
					rs.getString(7), rs.getString(8), rs.getString(9) };
			model.addRow(newRow);
		}

		String sql = "select sum(supply), sum(surtax), sum(sellingprice), sum(marginsum) from product";
		rs = connection.prepareStatement(sql).executeQuery();
		if (rs.next()) {
			newRow = new Object[] { "합계", "", "", "", "", rs.getInt(1),
					rs.getInt(2), rs.getInt(3), "", rs.getInt(4) };
			model.addRow(newRow);
			System.out.println("???");
		}
	}

	private void initialize() {
		setTitle("출력1");
		setBounds(100, 100, 800, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		title = new JLabel("판 매 금 액 순 위");
		title.setFont(new Font("굴림", Font.BOLD, 24));
		title.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(title, BorderLayout.NORTH);

		scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);

		table = new JTable();
		table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
				"\uC21C\uC704", "\uC81C\uD488\uCF54\uB4DC",
				"\uC81C\uD488\uBA85", "\uC81C\uD488\uB2E8\uAC00",
				"\uD310\uB9E4\uC218\uB7C9", "\uACF5\uAE09\uAC00\uC561",
				"\uBD80\uAC00\uC138\uC561", "\uD310\uB9E4\uAE08\uC561",
				"\uB9C8\uC9C4\uC728", "\uB9C8\uC9C4\uC561" }));
		scrollPane.setViewportView(table);

		setVisible(true);
	}
}
